/***
 * Copyright (c) 2021-2031 murenchao
 * fig is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.software.fig.core

import com.google.gson.Gson
import cool.taomu.software.fig.configure.ConfigureManage
import cool.taomu.software.fig.entity.FigData
import cool.taomu.software.fig.mqtt.broker.MQTTBroker
import cool.taomu.software.fig.mqtt.utils.CommonUtils
import org.apache.commons.cli.CommandLine
import org.apache.commons.cli.DefaultParser
import org.apache.commons.cli.Options
import org.slf4j.LoggerFactory

import static cool.taomu.software.fig.configure.ConfigureManage.*

class FigCore {
    val static LOG = LoggerFactory.getLogger(FigCore);

    def static startClient(String[] args, Class<?> zlass) {
        Runtime.runtime.addShutdownHook(new Thread() {
            override run() {
                CommonUtils.shutdown();
            }
        })
        var parser = new DefaultParser();
        var options = new Options();
        options.addOption("f", "file", true, "");
        var CommandLine commandLine = parser.parse(options, args);
        if (commandLine.hasOption('f')) {
            var file = commandLine.getOptionValue('f');
            LOG.info("设置配置文件：{}", file);
            ConfigureManage.configPath = file;
        }
        var service = new CreateMqttClient().create(zlass);
        service.forEach[client|CommonUtils.exec(client)]
    }

    def static startServer(String[] args) {
        Runtime.runtime.addShutdownHook(new Thread() {
            override run() {
                CommonUtils.shutdown();
            }
        })
        var parser = new DefaultParser();
        var options = new Options();
        options.addOption("f", "file", true, "");
        var CommandLine commandLine = parser.parse(options, args);
        if (commandLine.hasOption('f')) {
            var file = commandLine.getOptionValue('f');
            LOG.info("设置配置文件：{}", file);
            ConfigureManage.configPath = file;
        }
        MQTTBroker.start();
    }
    
    
    def static void main(String[] args) {
        var fd = new FigData();
        println(new Gson().toJson(fd));
    }
}
